{include="header"}

{if condition="$fsc->articulo"}
<script type="text/javascript">
{if condition="$GLOBALS['config2']['margin_method'] == 'CST'"} {* Margen sobre el precio de coste *}
   function margencalc(icst, ipvp)
   {
      if(icst == 0)
      {
         return '-';
      }
      else
      {
         return (ipvp-icst)/icst;
      }
   }
   function pvpcalc(icst, imc)
   {
      return icst*(1+imc);
   }
{else} {* Default: sobre el precio de venta *}
   function margencalc(icst, ipvp)
   {
      if(ipvp == 0)
      {
         return '-';
      }
      else
      {
         return (ipvp-icst)/ipvp;
      }
   }
   function pvpcalc(icst, imv)
   {
      if( (1-imv) == 0)
      {
         return '-';
      }
      else
      {
         return icst/(1-imv);
      }
   }
{/if}
   function recalcular()
   {
      var coste = parseFloat( $("#coste").val() );
      var iva = parseFloat( $("#iva").val() );
      var pvp = parseFloat( $("#pvp").val() );
      var npvp = 0;
      var dto = 0;
      for(var i=0; i<100; i++)
      {
         if( $("#dto_"+i) )
         {
            dto = parseFloat( $("#dto_"+i).val() );
            if( isNaN(dto) )
               dto = 0;
            
            npvp = pvp * (100 - dto)/100;
            $("#pvp_"+i).val( npvp );
            $("#pvpi_"+i).val( pvp * (100 - dto)/100 * (100 + iva)/100 );
            $("#margen_"+i).val( margencalc( coste, npvp ) * 100  );
         }
      }
   }
   function cambiar_pvp()
   {
      var coste = parseFloat( $("#coste").val() );
      var iva = parseFloat( $("#iva").val() );
      var pvp = parseFloat( $("#pvp").val() );
      $("#pvpi").val( pvp * (100 + iva)/100 );
      
      var margen = 0;
      if(pvp != 0)
      {
         margen = margencalc(coste, pvp) * 100;
      }
      
      if( isNaN(margen) )
         margen = 0;
      
      $("#margen").val(margen);
      
      recalcular();
   }
   function cambiar_margen()
   {
      var margen = parseFloat( $("#margen").val() );
      var coste = parseFloat( $("#coste").val() );
      var iva = parseFloat( $("#iva").val() );
      var pvp = pvpcalc(coste, margen/100.0);
      $("#pvp").val( pvp );
      $("#pvpi").val( pvp * (100 + iva)/100 );
      
      recalcular();
   }
   function cambiar_pvpi()
   {
      var coste = parseFloat( $("#coste").val() );
      var iva = parseFloat( $("#iva").val() );
      var pvpi = parseFloat( $("#pvpi").val() );
      $("#pvp").val( (100 * pvpi) / (100 + iva) );
      var pvp = parseFloat( $("#pvp").val() );
      $("#margen").val( margencalc( coste, pvp ) * 100 );
      recalcular();
   }
   function ajustar_dto()
   {
      var coste = parseFloat( $("#coste").val() );
      var pvp = parseFloat( $("#pvp").val() );
      var iva = parseFloat( $("#iva").val() );
      var npvp = 0;
      var dto = 0;
      for(var i=0; i<100; i++)
      {
         if( $("#dto_"+i) )
         {
            dto = parseFloat( $("#dto_"+i).val() );
            npvp = pvp * (100 - dto)/100;
            $("#pvp_"+i).val( npvp );
            $("#pvpi_"+i).val( npvp * (100 + iva)/100 );
            $("#margen_"+i).val( ( margencalc(coste, npvp) ) * 100  );
         }
      }
   }
   function ajustar_margen()
   {
      var coste = parseFloat( $("#coste").val() );
      var pvp = parseFloat( $("#pvp").val() );
      var iva = parseFloat( $("#iva").val() );
      var margen = 0;
      var npvp = 0;
      for(var i=0; i<3; i++)
      {
         if( $("#margen_"+i) )
         {
            margen = parseFloat( $("#margen_"+i).val() );
            npvp = parseFloat( pvpcalc(coste, margen/100.0) );
            $("#pvp_"+i).val( npvp );
            $("#pvpi_"+i).val( npvp * (100 + iva)/100 );
            $("#dto_"+i).val( 100 * ( pvp - npvp ) / pvp );
         }
      }
   }
   function ajustar_pvp()
   {
      var coste = parseFloat( $("#coste").val() );
      var pvp = parseFloat( $("#pvp").val() );
      var iva = parseFloat( $("#iva").val() );
      var npvp = 0;
      for(var i=0; i<100; i++)
      {
         if( $("#pvp_"+i) )
         {
            npvp = parseFloat( $("#pvp_"+i).val() );
            
            if(pvp == 0)
            {
               $("#pvp").val(npvp);
               cambiar_pvp();
               pvp = parseFloat( $("#pvp").val() );
               iva = parseFloat( $("#iva").val() );
            }
            
            $("#dto_"+i).val( 100 - (100*npvp)/pvp );
            $("#pvpi_"+i).val( npvp * (100 + iva)/100 );
            $("#margen_"+i).val( margencalc(coste, npvp) * 100  );
         }
      }
   }
   function ajustar_pvpi()
   {
      var coste = parseFloat( $("#coste").val() );
      var pvp = parseFloat( $("#pvp").val() );
      var iva = parseFloat( $("#iva").val() );
      var npvp = 0;
      var npvpi = 0;
      for(var i=0; i<100; i++)
      {
         if( $("#pvp_"+i) )
         {
            npvpi = parseFloat( $("#pvpi_"+i).val() );
            
            if(parseFloat($("#pvpi").val()) == 0)
            {
               $("#pvpi").val(npvpi);
               cambiar_pvpi();
               pvp = parseFloat( $("#pvp").val() );
               iva = parseFloat( $("#iva").val() );
            }
            
            npvp = (100*npvpi)/(100+iva);
            $("#pvp_"+i).val( npvp );
            $("#dto_"+i).val( 100 - (100*npvp)/pvp );
            $("#margen_"+i).val( margencalc(coste, npvp) * 100  );
         }
      }
   }
   function show_div(name)
   {
      $("#div_generales").hide();
      $("#div_precios").hide();
      $("#div_stock").hide();
      $("#div_equivalentes").hide();
      $("#b_generales").removeClass('active');
      $("#b_precios").removeClass('active');
      $("#b_stock").removeClass('active');
      $("#b_equivalentes").removeClass('active');
      
      if(name == 'precios')
      {
         $("#div_precios").show();
         $("#b_precios").addClass('active');
         cambiar_pvp();
      }
      else if(name == 'stock')
      {
         $("#div_stock").show();
         $("#b_stock").addClass('active');
      }
      else if(name == 'equivalentes')
      {
         $("#div_equivalentes").show();
         $("#b_equivalentes").addClass('active');
      }
      else
      {
         $("#div_generales").show();
         $("#b_generales").addClass('active');
      }
   }
   $(document).ready(function() {
      show_div(window.location.hash.substring(1));
      $("#b_generales").click(function(event) {
         event.preventDefault();
         show_div('generales');
      });
      $("#b_precios").click(function(event) {
         event.preventDefault();
         show_div('precios');
      });
      $("#b_stock").click(function(event) {
         event.preventDefault();
         show_div('stock');
      });
      $("#b_equivalentes").click(function(event) {
         event.preventDefault();
         show_div('equivalentes');
      });
      $("#b_eliminar_articulo").click(function(event) {
         event.preventDefault();
         if( confirm("¿Estas seguro de que deseas eliminar este articulo?") )
            window.location.href = "{$fsc->ppage->url()}&delete={function="urlencode($fsc->articulo->referencia)"}";
      });
      $("#b_imagen").click(function(event) {
         event.preventDefault();
         $("#modal_articulo_imagen").modal('show');
      });
   });
</script>

<div class="container-fluid" style="margin-top: 10px; margin-bottom: 10px;">
   <div class="row">
      <div class="col-sm-10">
         <a href="{$fsc->url()}" class="btn btn-sm btn-default" title="Recargar la página">
            <span class="glyphicon glyphicon-refresh"></span>
         </a>
         <a href="{$fsc->ppage->url()}" class="btn btn-sm btn-default">
            <span class="glyphicon glyphicon-arrow-left"></span> &nbsp; Todos los artículos
         </a>
         <a href="#" id="b_imagen" class="btn btn-sm btn-default">
            <span class="glyphicon glyphicon-picture"></span> &nbsp; Imagen
         </a>
      </div>
      <div class="col-sm-2 text-right">
         <a href="#" id="b_eliminar_articulo" class="btn btn-sm btn-danger">
            <span class="glyphicon glyphicon-trash"></span> &nbsp; Eliminar
         </a>
      </div>
   </div>
</div>

<ul class="nav nav-tabs" role="tablist">
   <li id="b_generales"><a href="#">Datos generales</a></li>
   <li id="b_precios"><a href="#precios">Precios</a></li>
   <li id="b_stock"><a href="#stocks">Stock</a></li>
   {if condition="$fsc->equivalentes"}
   <li id="b_equivalentes"><a href="#equivalentes">Equivalentes</a></li>
   {/if}
   <li class="dropdown">
      <a class="dropdown-toggle" data-toggle="dropdown">
         Buscar en... <span class="caret"></span>
      </a>
      <ul class="dropdown-menu" role="menu">
      {loop="$fsc->extensions"}
         {if condition="$value->type=='button'"}
         <li>
            <a href="index.php?page={$value->from}&ref={function="urlencode($fsc->articulo->referencia)"}">
               <span class="glyphicon glyphicon-list"></span> &nbsp; {$value->text}
            </a>
         </li>
         {/if}
      {/loop}
      </ul>
   </li>
</ul>

<form action="{$fsc->url()}" method="post" class="post">
   <input type="hidden" name="referencia" value="{$fsc->articulo->referencia}"/>
   <div id="div_generales" class="container-fluid">
      <div class="row" style="padding-top: 10px;">
         <div class="form-group col-lg-3 col-md-3 col-sm-3">
            Referencia:
            <input class="form-control" type="text" name="nreferencia" value="{$fsc->articulo->referencia}" maxlength="18" autocomplete="off"/>
         </div>
         <div class="form-group col-lg-6 col-md-6 col-sm-6">
            Descripción:
            <input class="form-control" type="text" name="descripcion" value="{$fsc->articulo->descripcion}" autocomplete="off"/>
         </div>
         <div class="form-group col-lg-3 col-md-3 col-sm-3">
            <a href="{$fsc->familia->url()}">Familia</a>:
            <select class="form-control" name="codfamilia">
            {loop="$fsc->familia->all()"}
               {if condition="$value->codfamilia===$fsc->articulo->codfamilia"}
               <option value="{$value->codfamilia}" selected="selected">{$value->descripcion}</option>
               {else}
               <option value="{$value->codfamilia}">{$value->descripcion}</option>
               {/if}
            {/loop}
            </select>
         </div>
         <div class="form-group col-lg-4 col-md-4 col-sm-4">
            Código de barras:
            <input class="form-control" type="text" name="codbarras" value="{$fsc->articulo->codbarras}" autocomplete="off"/>
         </div>
         <div class="col-lg-8 col-md-8 col-sm-8">
            <div class="checkbox-inline">
               <label>
                  <input type="checkbox" name="bloqueado" value="TRUE"{if condition="$fsc->articulo->bloqueado"} checked="checked"{/if}/>
                  Bloqueado
               </label>
            </div>
            <div class="checkbox-inline">
               <label>
                  <input type="checkbox" name="secompra" value="TRUE"{if condition="$fsc->articulo->secompra"} checked="checked"{/if}/>
                  Se compra
               </label>
            </div>
            <div class="checkbox-inline">
               <label>
                  <input type="checkbox" name="sevende" value="TRUE"{if condition="$fsc->articulo->sevende"} checked="checked"{/if}/>
                  Se vende
               </label>
            </div>
            <div class="checkbox-inline">
               <label>
                  <input type="checkbox" name="publico" value="TRUE"{if condition="$fsc->articulo->publico"} checked="checked"{/if}/>
                  Público
               </label>
            </div>
         </div>
      </div>
      <div class="row">
         <div class="form-group col-lg-6 col-md-6 col-sm-6">
            Código de equivalencia:
            <input class="form-control" type="text" name="equivalencia" value="{$fsc->articulo->equivalencia}" autocomplete="off"/>
            <p class="help-block">Dos o más artículos son equivalentes si tienen el mismo código de equivalencia.</p>
         </div>
         <div class="form-group col-lg-6 col-md-6 col-sm-6">
            <label>
               <input type="checkbox" name="destacado" value="TRUE"{if condition="$fsc->articulo->destacado"} checked="checked"{/if}/>
               Destacar frente a productos equivalentes
            </label>
         </div>
      </div>
      <div class="row">
         <div class="form-group col-lg-3 col-md-3 col-sm-3">
            Stock:
            <input class="form-control" type="text" name="stockfis" value="{$fsc->articulo->stockfis}" disabled="disabled"/>
         </div>
         <div class="form-group col-lg-3 col-md-3 col-sm-3">
            Stock mínimo:
            <input class="form-control" type="number" name="stockmin" value="{$fsc->articulo->stockmin}" autocomplete="off"/>
         </div>
         <div class="form-group col-lg-3 col-md-3 col-sm-3">
            Stock máximo:
            <input class="form-control" type="number" name="stockmax" value="{$fsc->articulo->stockmax}" autocomplete="off"/>
         </div>
         <div class="form-group col-lg-3 col-md-3 col-sm-3">
            <label>
               <input type="checkbox" name="controlstock" value="TRUE"{if condition="$fsc->articulo->controlstock"} checked="checked"{/if}/>
               Permitir ventas sin stock
            </label>
         </div>
         <div class="form-group col-lg-10 col-md-10 col-sm-10">
            Observaciones:
            <textarea class="form-control" name="observaciones" rows="3">{$fsc->articulo->observaciones}</textarea>
         </div>
         <div class="col-lg-2 col-md-2 col-sm-2 text-right">
            <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
               <span class="glyphicon glyphicon-floppy-disk"></span> &nbsp; Guardar
            </button>
         </div>
      </div>
   </div>
</form>

<form action="{$fsc->url()}#precios" method="post" class="form">
   <input type="hidden" name="referencia" value="{$fsc->articulo->referencia}"/>
   <input type="hidden" id="iva" name="iva" value="{$fsc->articulo->get_iva()}"/>
   <div id="div_precios">
      <div class="container-fluid" style="margin-top: 10px;">
         <div class="row">
            <div class="col-md-4">
               <div class="form-group">
                  PVP:
                  <input type="text" class="form-control" id="pvp" name="pvp" value="{$fsc->articulo->pvp}" autocomplete="off" onkeyup="cambiar_pvp()" onclick="this.select()"/>
                  <p class="help-block">Último cambio de precio: {$fsc->articulo->factualizado}</p>
               </div>
            </div>
            <div class="col-md-4">
               <div class="form-group">
                  <a href="{$fsc->impuesto->url()}">IVA</a>:
                  <select class="form-control" name="codimpuesto">
                  {loop="$fsc->impuesto->all()"}
                     {if condition="$value->codimpuesto==$fsc->articulo->codimpuesto"}
                     <option value="{$value->codimpuesto}" selected="selected">{$value->descripcion}</option>
                     {else}
                     <option value="{$value->codimpuesto}">{$value->descripcion}</option>
                     {/if}
                  {/loop}
                  </select>
               </div>
            </div>
            <div class="col-md-4">
               <div class="form-group">
                  PVP+IVA:
                  <input type="text" class="form-control" id="pvpi" name="pvpiva" value="{$fsc->articulo->pvp_iva()}" autocomplete="off" onkeyup="cambiar_pvpi()" onclick="this.select()"/>
               </div>
            </div>
         </div>
         <div class="row">
            <div class="col-md-4">
               <div class="form-group">
                  Precio de Coste:
                  {if condition="$fsc->articulo->secompra AND $GLOBALS['config2']['cost_is_average']"}
                  <input type="text" name="coste" id="coste" class="form-control" value="{$fsc->articulo->preciocoste()}" disabled="disabled">
                  {else}
                  <input type="text" name="preciocoste" id="coste" class="form-control" value="{$fsc->articulo->preciocoste()}">
                  {/if}
                  <p class="help-block">
                     Puede cambiar la configuración de precio de coste desde la <a href="index.php?page=admin_config2">configuración avanzada</a>.
                  </p>
               </div>
            </div>
            <div class="col-md-4">
               <div class="form-group">
                  Margen de ganancia:
                  <input type="text" class="form-control" id="margen" name="margen" value="0" autocomplete="off" onkeyup="cambiar_margen()" onclick="this.select()"/>
               </div>
            </div>
         </div>
      </div>
      
      <div class="table-responsive">
         <table class="table table-hover">
            <thead>
               <tr>
                  <th class="text-left">Tarifa</th>
                  <th class="text-right">Dto (%)</th>
                  <th class="text-right">
                     {if condition="$GLOBALS['config2']['margin_method'] == 'CST'"}
                     Margen (% sobre Coste)
                     {else}
                     Margen (% de Ventas)
                     {/if}
                  </th>
                  <th class="text-right">PVP</th>
                  <th class="text-right">PVP+IVA</th>
               </tr>
            </thead>
            {loop="$fsc->articulo->get_tarifas(TRUE)"}
            <tr>
               <td>
                  <div class="form-control"><a href="{$value->url()}">{$value->nombre}</a></div>
               </td>
               <td>
                  <input type="hidden" name="id_{$counter}" value="{$value->id}"/>
                  <input type="hidden" name="codtarifa_{$counter}" value="{$value->codtarifa}"/>
                  <input type="number" step="any" class="form-control text-right" id="dto_{$counter}" name="dto_{$counter}" value="{$value->descuento}" autocomplete="off" onchange="ajustar_dto()" onkeyup="ajustar_dto()" onclick="this.select()"/>
               </td>
               <td>
                  <input type="number" step="any" class="form-control text-right" id="margen_{$counter}" name="margen_{$counter}" value="" autocomplete="off" onchange="ajustar_margen()" onkeyup="ajustar_margen()" onclick="this.select()"/>
               </td>
               <td>
                  <input type="number" step="any" class="form-control text-right" id="pvp_{$counter}" name="pvp_{$counter}" value="{$value->pvp()}" autocomplete="off" onchange="ajustar_pvp()" onkeyup="ajustar_pvp()" onclick="this.select()"/>
               </td>
               <td>
                  <input type="number" step="any" class="form-control text-right" id="pvpi_{$counter}" name="pvpiva_{$counter}" value="{$value->pvp_iva()}" autocomplete="off" onchange="ajustar_pvpi()" onkeyup="ajustar_pvpi()" onclick="this.select()"/>
               </td>
            </tr>
            {/loop}
            <tr>
               <td colspan="6" class="text-center">
                  <a class="btn btn-sm btn-block btn-success" href="{$fsc->ppage->url()}#tarifas">Nueva tarifa</a>
               </td>          
            </tr>
         </table>
      </div>
      <div class="container-fluid">
         <div class="row">
            <div class="col-md-12 text-right">
               <br />
               <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
                  <span class="glyphicon glyphicon-floppy-disk"></span> &nbsp; Guardar
               </button>
            </div>
         </div>
      </div>
   </div>
</form>

<div id="div_stock">
   <div class="table-responsive">
      <table class="table table-hover">
         <thead>
            <tr>
               <th class="text-left">Almacén</th>
               <th class="text-right">Cantidad</th>
               <th class="text-right">Reservada</th>
               <th class="text-right">Disponible</th>
               <th class="text-right">Pendiente de recibir</th>
               <th class="text-right">Acción</th>
            </tr>
         </thead>
         {loop="$fsc->stocks"}
         <tr>
            <form action="{$fsc->url()}#stock" method="post" class="form">
               <input type="hidden" name="idstock" value="{$value->idstock}"/>
               <input type="hidden" name="almacen" value="{$value->codalmacen}"/>
               <input type="hidden" name="referencia" value="{$fsc->articulo->referencia}"/>
               <td><div class="form-control">{$value->codalmacen} {$value->nombre}</div></td>
               <td>
                  <input type="number" step="any" class="form-control text-right" name="cantidad" value="{$value->cantidad}" autocomplete="off"/>
               </td>
               <td><div class="form-control text-right">{$value->reservada}</div></td>
               <td><div class="form-control text-right">{$value->disponible}</div></td>
               <td><div class="form-control text-right">{$value->pterecibir}</div></td>
               <td class="text-right">
                  <button class="btn btn-sm btn-primary" type="submit" title="Guardar" onclick="this.disabled=true;this.form.submit();">
                     <span class="glyphicon glyphicon-floppy-disk"></span>
                  </button>
               </td>
            </form>
         </tr>
         {/loop}
         {if condition="$fsc->nuevos_almacenes"}
         <tr class="bg-info">
            <form action="{$fsc->url()}#stock" method="post" class="form">
               <input type="hidden" name="referencia" value="{$fsc->articulo->referencia}"/>
               <td>
                  <select class="form-control" name="almacen">
                     {loop="$fsc->nuevos_almacenes"}
                     <option value="{$value->codalmacen}">{$value->nombre}</option>
                     {/loop}
                  </select>
               </td>
               <td><input class="form-control text-right" type="number" step="any" name="cantidad" value="1" autocomplete="off"/></td>
               <td><div class="form-control text-right">0</div></td>
               <td><div class="form-control text-right">0</div></td>
               <td><div class="form-control text-right">0</div></td>
               <td class="text-right">
                  <button class="btn btn-sm btn-primary" type="submit" title="Guardar" onclick="this.disabled=true;this.form.submit();">
                     <span class="glyphicon glyphicon-floppy-disk"></span>
                  </button>
               </td>
            </form>
         </tr>
         {/if}
      </table>
   </div>
</div>

<div id="div_equivalentes">
   {if condition="$fsc->equivalentes"}
   <div class="table-responsive">
      <table class="table table-hover">
         <thead>
            <tr>
               <th class="text-left">Artículo</th>
               <th class="text-right">PVP+IVA</th>
               <th class="text-right">Stock</th>
            </tr>
         </thead>
         {loop="$fsc->equivalentes"}
         <tr>
            <td><a href="{$value->url()}">{$value->referencia}</a></td>
            <td class="text-right">{$fsc->show_precio($value->pvp_iva())}</td>
            <td class="text-right">{$value->stockfis}</td>
         </tr>
         {/loop}
      </table>
   </div>
   {else}
   <div class="alert alert-warning">No hay artículos equivalentes.</div>
   {/if}
</div>

<form action="{$fsc->url()}" enctype="multipart/form-data" method="post" class="form">
   <input type="hidden" name="referencia" value="{$fsc->articulo->referencia}"/>
   <input type="hidden" name="imagen" value="TRUE"/>
   <div class="modal fade" id="modal_articulo_imagen">
      <div class="modal-dialog">
         <div class="modal-content">
            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
               <h4 class="modal-title">Imagen</h4>
            </div>
            <div class="modal-body">
               {if condition="$fsc->articulo->imagen_url()"}
               <div class="thumbnail">
                  <img src="{$fsc->articulo->imagen_url()}" alt="{$fsc->articulo->referencia}"/>
               </div>
               {else}
               <div class="form-group">
                  Selecciona una imagen en formato PNG de tamaño inferior a 1MB.<br/>
                  <input type="file" name="fimagen" accept="image/gif, image/jpeg, image/png"/>
               </div>
               {/if}
            </div>
            <div class="modal-footer">
               {if condition="$fsc->articulo->imagen_url()"}
               <a class="btn btn-sm btn-danger" href="{$fsc->url()}&delete_img=TRUE">
                  <span class="glyphicon glyphicon-trash"></span> &nbsp; Eliminar
               </a>
               {else}
               <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
                  <span class="glyphicon glyphicon-floppy-disk"></span> &nbsp; Guardar
               </button>
               {/if}
            </div>
         </div>
      </div>
   </div>
</form>
{else}
<div class="text-center">
   <img src="view/img/fuuu_face.png" alt="fuuuuu"/>
</div>
{/if}

{include="footer"}